package com.zcoj.mail.service.impl;

import com.zcoj.mail.mapper.MailLogMapper;
import com.zcoj.mail.pojo.Mail;
import com.zcoj.mail.pojo.MailLog;
import com.zcoj.mail.service.EmailService;
import com.zcoj.mail.service.MailLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.concurrent.Executor;

/**
 * @author chenbin
 * @version 1.0
 * @description: TODO
 * @date 2023/3/4 23:16
 */
@Slf4j
@Service
public class EmailServiceImpl implements EmailService {
    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    private TemplateEngine templateEngine;

    @Autowired
    private MailLogMapper mailLogMapper;

    @Autowired
    private Executor asyncExecutor;

    @Autowired
    private MailLogService mailLogService;



    /**
     * @method: [sendSimpleMail]
     * @param: [mail]
     * @returns: void
     * @description:  类型SimpleMail
     * @author: chen.bin
     * @version: 1.0.0
     * @date 2023/3/4 23:31
     **/
    @Override
    @Async("asyncExecutor")
    public void sendSimpleMail(Mail mail) {
//        SimpleMailMessage message = new SimpleMailMessage();
//        //邮件主题
//        message.setSubject("ZCOJ");
//        //邮件发送者
//        message.setFrom(mail.getFrom());
//        //邮件接收者,可以有多个接收者
//        message.setTo(mail.getTo());
//        //邮件发送日期
//        message.setSentDate(new Date());
//        //邮件正文
//        message.setText(mail.getDate());
//        //发送邮件
//        javaMailSender.send(message);
    }

    /**
     * @method: [sendThymeleafLoginMail]
     * @param: [mail]
     * @returns: void
     * @description:  LoginMail
     * @author: chen.bin
     * @version: 1.0.0
     * @date 2023/3/4 23:31
     **/

    @Override
    @Async("asyncExecutor")
    public void sendThymeleafLoginMail(MailLog mailLog) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, StandardCharsets.UTF_8.name());
        helper.setSubject("ZCOJ邮箱登录验证码");
        helper.setFrom(mailLog.getFromMail());
        helper.setTo(mailLog.getToMail());
        helper.setSentDate(new Date());

        //绑定到mail.thymeleaf模板的数据
        Context context = new Context();
        context.setVariable("username", mailLog.getUsername());
        context.setVariable("code", mailLog.getCode());
        String process = templateEngine.process("loginMail.html", context);
        //setText()第二个参数:开启html模板
        helper.setText(process,true);
        try{
            javaMailSender.send(mimeMessage);
            mailLog.setIsOk(1);
            log.info("邮件发送成功---from:{}---->to:{}---code:{}",mailLog.getFromMail(),mailLog.getToMail(),mailLog.getCode());
        }catch (Exception e){

            mailLog.setIsOk(0);
            log.error("邮件发送失败---from:{}---->to:{}---code:{}----e:{}",mailLog.getFromMail(),mailLog.getToMail(),mailLog.getCode(),e.getMessage());
        }finally{
            mailLog.setType("LoginMail");
            mailLog.setStatus(1);
        }
        mailLogMapper.insert(mailLog);
    }

    /**
     * @method: [sendThymeleafRegisterMail]
     * @param: [mail]
     * @returns: void
     * @description:  RegisterMail
     * @author: chen.bin
     * @version: 1.0.0
     * @date 2023/3/4 23:32
     **/
    @Override
    @Async("asyncExecutor")
    public void sendThymeleafRegisterMail(MailLog mailLog) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true,StandardCharsets.UTF_8.name());
        helper.setSubject("ZCOJ邮箱注册验证码");
        helper.setFrom(mailLog.getFromMail());
        helper.setTo(mailLog.getToMail());
        helper.setSentDate(new Date());

        //绑定到mail.thymeleaf模板的数据
        Context context = new Context();
        context.setVariable("username", mailLog.getUsername());
        context.setVariable("code", mailLog.getCode());
        String process = templateEngine.process("registerMail.html", context);
        //setText()第二个参数:开启html模板
        helper.setText(process,true);
        try{
            javaMailSender.send(mimeMessage);
            mailLog.setIsOk(1);
            log.info("邮件发送成功---from{}---->to:{}---code:{}",mailLog.getFromMail(),mailLog.getToMail(),mailLog.getCode());
        }catch (Exception e){
            mailLog.setIsOk(0);
            log.error("邮件发送失败---from{}---->to:{}---code:{}",mailLog.getFromMail(),mailLog.getToMail(),mailLog.getCode());
        }finally{
            mailLog.setType("registerMail");
            mailLog.setStatus(1);
        }
        mailLogMapper.insert(mailLog);

    }
}
